/* 
 * Copyright TLR Team, Inc. All Rights Reserved.
 */

/* global AMap */

TLR.controllers.controller("MapViewCtrl", function ($scope) {
})
.controller("CarMapViewCtrl", function ($scope, VehicleSrv, CAR) {
    var w = $(window).width();
    var h = $(window).height();
    var map_container_id_name = "car-map-container";
    var map_container_id = "#" + map_container_id_name;
    $(map_container_id).width(w - 30);
    $(map_container_id).height(h - 135);
    $scope.carListTimer = setInterval(function () {
        // 3mins
        $scope.$broadcast(CAR.event.list);
    }, 3 * 1000 * 60);
    $scope.$on('$destroy', function () {
        // clear timer
        clearInterval($scope.carListTimer);
    });
    var map = $scope.carMap = new AMap.Map(map_container_id_name, {
        zoom: 10,
        center: [121.496548, 31.239432],
        animateEnable: false
    });
    $scope.$on(CAR.event.list, function () {
        VehicleSrv.positions(function (carListResp) {
            if (carListResp.code !== 0) {
                alert(carListResp.message);
                return;
            }
            map.clearMap();  // 清除地图覆盖物
            $scope.carList = carListResp.data;
            if ($scope.carListMock && $scope.carListMock.length > 0) {
                $scope.carList = $scope.carList.concat($scope.carListMock);
            }
            for (var i = 0; i < $scope.carList.length; ++i) {
                var car = $scope.carList[i];
                var img = document.createElement('img');
                $(img).addClass("dashboard-car");
                if (car.status === 1) {
                    $(img).attr("src", "app/images/car/in-service.svg");
                    $(img).addClass("in-service");
                } else if (car.status === 2) {
                    $(img).attr("src", "app/images/car/ready-service.svg");
                    $(img).addClass("ready-service");
                } else if (car.status === 3) {
                    $(img).attr("src", "app/images/car/stop-service.svg");
                    $(img).addClass("stop-service");
                } else {
                    continue;
                }
                var marker = new AMap.Marker({
                    map: map,
                    position: [car.lng, car.lat],
                    offset: new AMap.Pixel(-20, -15),
                    content: img
                });
                marker.infoWindow = new AMap.InfoWindow({
                    isCustom: true, //使用自定义窗体
                    content: "<div class='car-info-window'><div class='car-info'>"
                    + "车牌:" + car.plate + "<br>"
                    + "状态:" + car.status_label + "<br>"
                    + "手机:" + car.mobile + "<br>"
                    + "</div></div>",
                    offset: new AMap.Pixel(6, -25)
                });
                AMap.event.addListener(marker, 'click', function (evt) {
                    if (evt.target.infoWindow.getIsOpen())
                        evt.target.infoWindow.close();
                    else
                        evt.target.infoWindow.open(map, evt.target.getPosition());
                });
            }
            setTimeout(function () {
                map.setFitView();
            }, 500);
        });
    });
    /*
     CarMockSrv.list(function (resp) {
     $scope.carListMock = resp.data;
     $scope.$broadcast(CAR.event.list);
     });//*/
    $scope.$broadcast(CAR.event.list);
})
.controller("HealthMapViewCtrl", function ($scope, HEALTH, EBanDeviceSrv, EBanDeviceMockSrv) {
    var w = $(window).width();
    var h = $(window).height();
    var map_container_id_name = "health-map-container";
    var map_container_id = "#" + map_container_id_name;
    $(map_container_id).width(w - 30);
    $(map_container_id).height(h - 135);
    var map = new AMap.Map(map_container_id_name, {
        zoom: 10,
        center: [121.496548, 31.239432],
        animateEnable: false
    });
    $scope.$on(HEALTH.event.list, function () {
        EBanDeviceSrv.fast_map_info(function (healthResp) {
            if (healthResp.code !== 0) {
                alert(healthResp.message);
                return;
            }
            map.clearMap();  // 清除地图覆盖物
            $scope.healthDevices = healthResp.data;
            if ($scope.healthDevicesMock && $scope.healthDevicesMock.length > 0) {
                $scope.healthDevices = $scope.healthDevices.concat($scope.healthDevicesMock);
            }
            for (var i = 0; i < $scope.healthDevices.length; ++i) {
                var healthDevice = $scope.healthDevices[i];
                if (healthDevice.status === 0 || healthDevice.enabled === false) {
                    continue;
                }
                var div = document.createElement('div');
                $(div).css("border-radius", "5px")
                .css("background-color", $scope.getHealthColor(healthDevice.point))
                .css("width", "35px")
                .css("height", "20px")
                .css("color", "#fff")
                .css("text-align", "center");
                div.innerHTML = healthDevice.point;
                new AMap.Marker({
                    map: map,
                    position: [healthDevice.lng, healthDevice.lat],
                    offset: new AMap.Pixel(-30, -10),
                    content: div
                });
            }
            setTimeout(function () {
                map.setFitView();
            }, 500);
        });
    });

    // TODO Temp used
    /*
     EBanDeviceMockSrv.list(function (resp) {
     $scope.healthDevicesMock = resp.data;
     $scope.$broadcast(HEALTH.event.list);
     });//*/
    $scope.$broadcast(HEALTH.event.list);
})
.controller("AirMapViewCtrl", function ($scope, AirDeviceSrv, HIKESrv, AIR) {
    $scope.cityPM25 = 0;
    var w = $(window).width();
    var h = $(window).height();
    //console.log("w:" + w, "h:" + h);
    var map_container_id_name = "air-map-container";
    var map_container_id = "#" + map_container_id_name;
    $(map_container_id).width(w - 30);
    $(map_container_id).height(h - 190);
    var map = new AMap.Map(map_container_id_name, {
        zoom: 10,
        center: [121.496548, 31.239432],
        animateEnable: false
    });
    $scope.airListTimer = setInterval(function () {
        // 3mins
        $scope.$broadcast(AIR.event.list);
    }, 3 * 60 * 1000);
    $scope.$on('$destroy', function () {
        // clear timer
        clearInterval($scope.airListTimer);
    });
    $scope.createMapElement = function (pm25_measure, lang, lati) {
        var div = document.createElement('div');
        $(div).css("border-radius", "5px")
        .css("background-color", $scope.getColorOfPM25(pm25_measure))
        .css("width", "35px")
        .css("height", "20px")
        .css("color", "#fff")
        .css("text-align", "center");
        div.innerHTML = pm25_measure;
        new AMap.Marker({
            map: map,
            position: [lang, lati],
            offset: new AMap.Pixel(-30, -10),
            content: div
        });
    };
    $scope.$on(AIR.event.list, function () {
        map.clearMap();  // 清除地图覆盖物
        HIKESrv.all_data(function (res) {
            if (res.code !== 0) {
                alert(res.message);
                return;
            }
            $scope.hikeDataList = res.data;
            for (var i = 0; i < $scope.hikeDataList.length; ++i) {
                var device = $scope.hikeDataList[i];
                $scope.createMapElement(device.pm25, device.lang, device.lati);
            }
            setTimeout(function () {
                map.setFitView();
            }, 500);
        });

        AirDeviceSrv.aqi(function (res) {
            if (res.code === 0) {
                $scope.cityPM25 = res.data.aqi.city.pm25;
            }
        });

        AirDeviceSrv.last_data(function (deviceResponse) {
            if (deviceResponse.code !== 0) {
                alert(deviceResponse.message);
                return;
            }
            $scope.cityPM25 = 0;
            $scope.airDevices = deviceResponse.data;
            if ($scope.airDevicesMock && $scope.airDevicesMock.length > 0) {
                $scope.airDevices = $scope.airDevices.concat($scope.airDevicesMock);
            }
            for (var i = 0; i < $scope.airDevices.length; ++i) {
                var device = $scope.airDevices[i];
                if (device.expired)
                    continue;
                $scope.cityPM25 = device.pm25_outdoor;
                $scope.createMapElement(device.pm25_measure, device.device_lang, device.device_lati);
            }
            setTimeout(function () {
                map.setFitView();
            }, 500);
        });
    });

    // Temp used
    /*
     AirDeviceMockSrv.list(function (resp) {
     $scope.airDevicesMock = resp.data;
     $scope.$broadcast(AIR.event.list);
     });//*/
    $scope.$broadcast(AIR.event.list);
});

